﻿///<reference path="../../../assets/js/jquery/jquery.js"/>
/*----------------------------------------------------------------------------
												Client logic for Admin/Pages/Votes.aspx
------------------------------------------------------------------------------*/
var Vote = function() {
	//private area
	var _ajaxDefaultOpts = {
		type: 'post',
		cache: false,
		dataType: 'json',
		contentType: 'application/json;charset=utf-8'
	};
	var _defaultDialogOpts = {
		modal: true,
		width: 600,
		height: 400,
		resizable: false,
		overlay: { opacity: 0.5, background: 'black' }
	};
	var initFlexigrid = function() {
		$("#" + Vote.FlexigridID).flexigrid
			(
  		{
  			url: Vote.Asmx + 'GetVotes',
  			urlType: "asmx",
  			dataType: 'json',
  			contentType: "application/json;charset=utf-8",
  			colModel: [
  			{ display: '标题', name: 'title', width: 180, sortable: true, align: 'center' },
  			{ display: '描述', name: 'description', width: 180, sortable: false, align: 'left' },
  			{ display: '创建人', name: 'author', width: 120, sortable: false, align: 'left' },
  			{ display: '是否多选', name: 'ismultiple', width: 60, sortable: false, align: 'left' }
					],
  			buttons: [
  			{ name: 'Add', display: '创建投票', bclass: 'add', onpress: function(_params) {
  				Vote.PreEdit(-1, _params);
  			}
  			},
  			{ name: 'Delete', display: '删除', bclass: 'delete', onpress: function(_params) {
  				Vote.Del(_params);
  			}
  			},
  			{ separator: true }
					],
  			searchitems: [
  			{ display: 'Title', name: 'Title' },
  			{ display: 'Description', name: 'Description', isdefault: true }
					],
  			sortname: "title",
  			sortorder: "asc",
  			usepager: true,
  			title: 'Votes',
  			useRp: true,
  			rp: 20,
  			showTableToggleBtn: false,
  			width: 700,
  			height: 200,
  			preProcess: function(data) {
  				return data.d;
  			},
  			onRowDblClick: function(_params) {
  				Vote.PreEdit(_params.id, _params);
  			}
  		}
		);
	};
	var initDatepicker = function() {
		$("#txtDateRange").datepicker({
			showOn: 'both',
			rangeSelect: true,
			minDate: new Date(2008, 9 - 1, 1),
			maxDate: new Date(2010, 1 - 1, 1),
			buttonImage: Vote.WebRoot + "admin/assets/img/calendar.gif",
			buttonImageOnly: true,
			rangeSeparator: '至'
		});
	}; // /initDatepicker
	// /private area

	//public area
	return {
		Asmx: null,
		FlexigridID: null,
		WebRoot: null,
		Init: function(opts) {
			Vote.Asmx = opts.Asmx;
			Vote.FlexigridID = opts.FlexigridID;
			Vote.WebRoot = opts.WebRoot;
			//load votes
			initFlexigrid();
			//initDatepicker
			initDatepicker();
		}, // /Init
		PreEdit: function(id, opts) {
			var dialogOpts = _defaultDialogOpts;
			dialogOpts.close = function() {
				$("#toolsWrapper").append($("#editBox").dialog("destroy").remove());
			};
			if (id == -1 && $("#editBox").html() != "") {
				$("#editBox").show().dialog(dialogOpts);
				return;
			};
			if (id != -1) {
				dialogOpts.close = function() {
					$("#toolsWrapper").append($("#editBox").dialog("destroy").empty().remove());
				};
			};
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = Vote.Asmx + "PreEdit";
			ajaxopts.data = $.toJSON({ Params: { id: id, user: $("#txtUser").val()} });
			ajaxopts.success = function(msg) {
				msg = msg.d;
				$("#editBox").html(msg.Body).show().dialog(dialogOpts);
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		}, // /PreEdit
		PreAddVoteItem: function(opts) {
			var dialogOpts = _defaultDialogOpts;
			dialogOpts.close = function() {
				$("#toolsWrapper").append($("#postsSelector").dialog("destroy"));
			};
			$("#postsSelector").show().dialog(dialogOpts);
			//Fetch Category dropdown list
			if ($("#ddlCategoryWrapper").html() == "") {
				var communities = $("#ddlCategoryWrapper0").children().clone().attr("id", "ddlCategory").attr("name", "VPostCategory");
				$("#ddlCategoryWrapper").append(communities);
			};
			//focus to ddlCParent
			$("#ddlCategory").select();
		}, // /PreAddVoteItem
		AddVoteItem: function(opts) {
			var voteid = $("#txtDataKey").val();
			var data = $("input:checked", "#voteItemCandidate");
			data = data.serializeArray();
			data.push({ name: 'VoteID', value: voteid });

			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = Vote.Asmx + "AddVoteItems";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				$("#btnAddPostsToVote").attr("value", "添 加").attr("disabled", false);
				msg = msg.d;
				if (msg.IsOk) {
					//close the dialog
					$("#postsSelector").dialog("close");

					if (msg.Context.VoteItemList) {
						$("#voteItemListWrapper").html(msg.Context.VoteItemList);
					}
				} else {
					alert(msg.Body);
				};
			};

			$.ajax(ajaxopts);
			ajaxopts = null;
			$("#btnAddPostsToVote").attr("value", "添加ing..").attr("disabled", true);
		}, // /AddVoteItem
		FindPostsForVote: function(opts) {
			if ($("#ddlCategory").val() == "-1") {
				alert("请选择新闻分类");
				$("#ddlCategory").select();
				return;
			};
			if ($("#txtDateRange").val() == "") {
				alert("请选择日期范围");
				return;
			};
			var data = {};
			data.Category = $("#ddlCategory").val();
			data.DateRange = $("#txtDateRange").val();
			data.Top = 15; //top 15 records
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = Vote.Asmx + "SelectPosts";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				$("#btnSearch").attr("value", "查 询").attr("disabled", false);
				msg = msg.d;
				if (msg.IsOk) {
					$("#voteItemCandidateWrapper").html(msg.Body);
				} else {
					alert(msg.Body);
				};
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
			$("#btnSearch").attr("value", "查询ing..").attr("disabled", true);
		}, // /FindPostsForVote
		Save: function(opts) {
			//1,get all fields to save
			var data = $(":input").filter("[name^='Vote'],[name='user']").serializeArray();
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = Vote.Asmx + "Update";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					$("#statusbar").html("保存成功").css({ color: 'blue' }).show().fadeOut(2000, function() {
						//focus to first input
						$("#txtVTitle").select();
					}); //.fadeOut(3000, _defaultDialogOpts.close);
					$("#" + Vote.FlexigridID).flexReload();
				} else {
					$("#statusbar").html(msg.Body).css({ color: 'red' }).show().fadeOut(3000);
					//Todo:highlight error elements
				}
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		}, // /Save
		Del: function(opts) {
			var keys = $.map($.makeArray(opts.rows), function(node, i) {
				return node.id.substring(3); //remove id prefix 'row'
			});
			if (keys.length == 0) {
				alert("没有选中任何行");
				return;
			}
			if (!window.confirm("确认删除选定的" + keys.length + "行记录么?")) {
				return;
			};
			//Request to server
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = Vote.Asmx + "Delete";
			ajaxOpts.data = $.toJSON({ Params: { keys: keys.join('|'), user: $("#txtUser").val()} });
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					$("#" + Vote.FlexigridID).flexReload();
				} else {
					alert(msg.Body);
				}
			};
			$.ajax(ajaxOpts);
			ajaxOpts = null;
		}, // /Del
		DelVoteItem: function(opts) {
			if (!window.confirm("确认删除此投票项么?")) {
				return;
			};
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = Vote.Asmx + "DelVoteItem";
			ajaxopts.data = $.toJSON({ Params: { VoteID: $("#txtDataKey").val(), VoteItemID: opts.id} });
			ajaxopts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					if (msg.Context.VoteItemList) {
						$("#voteItemListWrapper").html(msg.Context.VoteItemList);
					}
				} else {
					alert(msg.Body);
				};
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		} // /DelVoteItem
	};

} ();